package com.capgemini.simpleappblanche.controller.home;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.capgemini.simpleappblanche.utils.SessionTools;

/**
 * login controller
 * Manage all method associated with login / logout connection
 * @author jcdorchies
 * 
 */
@Controller
@RequestMapping()
public class LoginController {

	@Autowired
	private SessionTools sessionTools;
	
	/**
	 * login succeeded
	 * @param model model
	 * @param auth auth
	 * @return view
	 */
	@RequestMapping(value="/loginsucceeded", method = RequestMethod.GET)
	public String loginsucceeded(ModelMap model,Authentication auth) {
 
	    //login and auth are in session in Authentication
	    
	    //retrieve some data from user and store them in session 
	    //TODO retrieve data from user to put them in session
	    
		return "redirect:/dashboard";
	}
 
	/**
	 * login failed
	 * @param model model
	 * @return view
	 */
	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
	public String loginfailed(ModelMap model) {
 
		model.addAttribute("errorLogin", "true");
		
		//TODO initialize screen like welcome
		
		return "welcome";
	}
 
	/**
	 * logout method
	 * @param request request
	 * @return view
	 */
	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request) {
		sessionTools.clearSession(request);
		return "redirect:/welcome";
 	}
}
